

#include "prefabInspector.h"
#include "renderer.h"
#include "input.h"

PrefabInspector::PrefabInspector(){

    bOpen=false;
    bPermanent=true;
    initialLocation=location;
    tabWidth=350;
    listHeight=64;
    listWidth=64;
    listDisplayMode=4;
    listColumns=4;
    level=0;

    textureID="icon_ball";
    }

PrefabInspector::~PrefabInspector(){}


void PrefabInspector::setup(){
ListButton::setup();

}

void PrefabInspector::update(double deltaTime){

        if (listButton.size()<input->prefabs.size()){
            ListButton::deselect(level);
            listButton.clear();
            assembleList();
        }

}

void PrefabInspector::mouseOver(){

ListButton::mouseOver();
}


void PrefabInspector::drawPlane(){

    Button::drawPlane();

    renderer->setupShading("color");
    if (bOpen){
        renderer->drawPlane(scale.x ,0.0, scale.x+tabWidth,renderer->screenY, Vector4f(0.4,0.4,0.4, 1.0));
    }

}


void PrefabInspector::mouseDrag(){}

void PrefabInspector::clickedLeft(){

    if (!bOpen){
        initialLocation=location;
        location.x-=tabWidth;
        bOpen=true;
        for (uint i=0;i<listButton.size();i++){
            listButton[i]->location.x-=tabWidth;
            listButton[i]->initialLocation.x=listButton[i]->location.x;
            }
        ListButton::clickedLeft();
    }
    else{
        for (uint i=0;i<listButton.size();i++)
            listButton[i]->location.x+=initialLocation.x-location.x;
        location=initialLocation;
        bOpen=false;
    }
}


void PrefabInspector::assembleList(){

        cout << "creating list..." << endl;

        for (int i=0;i<(int)input->prefabs.size();i++){
            renderer->actorInfo["12AssignButton"].actorReference->create();
            listButton.push_back(renderer->buttonList.back());

            listButton[i]->name=input->prefabs[i];
            listButton[i]->textureID="icon_base";
            listButton[i]->level=level+1;
            listButton[i]->bDrawName=true;
            listButton[i]->color=Vector4f(0,0,1,1.0);
            listButton[i]->bPermanent=true;
            listButton[i]->bDragable=true;
            listButton[i]->parent=this;
            listButton[i]->setup();
            placeButton(i,i);
            //set this because we want to drag buttons around!
            listButton[i]->initialLocation=listButton[i]->location;
        }

    cout << "our Button list is: "<< listButton.size() <<" elements long..." << endl;
}


void PrefabInspector::trigger(Actor* other){

    ListButton::trigger(other);
    if (other==scrollBar)
        return;

    cout << "placed prefab!" << endl;

    int prefNo=0;

    //clean up input->selectedActors
    input->deselectActors();
    //determine which button called us back
    for (int i=0;i<(int)listButton.size();i++)
        if (other==listButton[i])
            prefNo=i;

     input->loadPrefab("resources/prefabs/"+input->prefabs[prefNo]);


    //this should only be called when we drag'n'dropped the prefab somewhere
    if (!input->hudTarget){
        for (int i=0;i< (int)input->selectedActors.size();i++){
            if (input->selectedActors[i]->base==NULL)
                input->selectedActors[i]->setLocation(input->selectedActors[i]->location+ input->mouse3D);
            }
        }

}

void PrefabInspector::clickedRight(){}
void PrefabInspector::focusClick(){

ListButton::focusClick();
}

void PrefabInspector::deselect(int depth){

//ListButton::deselect(depth);
}

void PrefabInspector::create(){renderer->addButton(this);}
